<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Xcx\Model;

use Think\Model\RelationModel;

/**
 * Class UsersModel
 * @package Home\Model
 */
class UsersModel extends RelationModel {

    public function __construct() {
        parent::__construct();
        $this->user_id = session('user_id');
    }

    public function isPayByQuestionId($question_id,$user_id,$type = 0) {
        $question_id = $question_id ? $question_id : 0;
        //$user_id = M('question')->where('id = '.$question_id)->getField('user_id');
        $find = M('pay_log')
            ->where('status = 1 and question_id = '.$question_id. ' and user_id = '.$user_id.' and type = '.$type)->find();
        if($find){
            return true;
        }
        return false;
    }

    public function getAdoptionNum($question_id,$au_id) {
        $question_id = $question_id ? $question_id : 0;
        $au_id = $au_id ? $au_id : 0;
        $count = M('adoption')->where('question_id = '.$question_id.' and accountant_id = '.$au_id)->count();
        return $count;
    }

    public function getInfoById($user_id = '') {
        $user_id = $user_id ? $user_id : 0;
        $find =  M('users')->where('user_id = '.$user_id.' and is_lock = 0')->find();
        if($find){
            unset($find['password']);
            if($find['mobile'])
                $find['mobile'] = substr($find['mobile'],0,4).'****'.substr($find['mobile'],-3);
        }
        return $find;
    }

    public function updateInfo($user_id = '',$data = array()){
        $user_id = $user_id ? $user_id : 0;
        $res = array('status' => 0 ,'msg' => '请输入内容信息' );
        if(!$data)
            return $res;
        if($data['mobile']) {
            if(!check_mobile($data['mobile'])){
                $res['msg'] = '手机号格式不正确';
                return $res;
            }
            $exited = M('users')->where(" mobile = '" . $data['mobile'] . "' ")->count();
            if ($exited) {
                $res['msg'] = '手机号已存在';
                return $res;
            }
        }
        $bool = M('users')->where('user_id = '.$user_id.' ')->save($data);
        if($bool){
            $res['status'] = 1;
            $res['msg'] = '更新成功';
        }else{
            $res['msg'] = '更新失败';
        }
        return $res;
    }



    public function getAreaList(){
        //  获取省份
        $data = array();
        $province = M('region')->where(array('parent_id' => 0, 'level' => 1))->field('id,name')->select();
        foreach($province as $p){
            $city = M('region')->where(array('parent_id' => $p['id'], 'level' => 2))->field('id,name')->select();
            $temp = $p;
            foreach($city as $c){
                $temp['child'][] = $c;
            }
            $data[] = $temp;
        }
        return $data;
    }

}
